<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;

use App\Models\Baoming;

use App\Models\Popularity;
use App\Models\Singer;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;


class IndexController extends Controller
{
    protected function validator($data, $rules, $messages = [])
    {


        $validator = Validator::make($data, $rules, $messages);

        if ($validator->fails()) {

            foreach ($validator->messages()->all() as $message) {

                echo "<script>
alert('{$message}');
history.go(-1);
</script>";
                exit ();
            }
        }
        return $data;
    }


    public function index()
    {
        $data = [];

        return view('web.home2017');
    }

    public function about()
    {
        return view('web.about');
    }


    public function hot()
    {
        $singer = new Singer();
        $list = $singer->where('hot', 1)->get()->toArray();

//        $old = $Popularity->with('singer')->where('qishu', $qishu - 1)->get()->toArray();

        return view('web.hot', compact('list'));
    }

    public function juesai()
    {
        $singer = new Singer();

        $list = $singer->where('final', 1)->get()->toArray();

        return view('web.hot-12', compact('list'));
    }

    public function finals()
    {
        $singer = new Singer();

        $list = $singer->where(['rank' => 1, 'rank' => 2, 'rank' => 3])->get()->toArray();

        return view('web.hot-3', compact('list'));
    }

    public function singer($id)
    {
        $singer = new Singer();
        $info = $singer->find($id);


        return view('web.hot-3-detail', compact('info'));
    }


    public function getBaoming()
    {

        $source = Baoming::$source;


        return view('web.enter-personal', compact('source'));
    }


    public function postBaoming()
    {
        $params = Input::all();

        $rules = [
            'relname' => 'required',
            'song' => 'required',
            'email' => 'required|email',
            'mobile' => 'required',
            'source' => 'required',
            'age' => 'required|integer',
//            'clause' => 'required',
        ];

        $messages = [
            'relname.required' => '参赛者姓名不能为空',
            'age.required' => '主唱年龄不能为空',
            'age.integer' => '年龄必须为整数',
            'song.required' => '参赛曲目不能为空',
            'email.required' => '邮箱不能为空',
            'mobile.required' => '联系方式不能为空',
            'source.required' => '您从何渠道知晓本次活动',
            'clause.required' => '隐私条款必读',
        ];
        self::validator($params, $rules, $messages);

        $model = new Baoming();
        $model->type = isset($params['type']) ? $params['type'] : 1;
        $model->relname = isset($params['relname']) ? $params['relname'] : '';
        $model->relname2 = isset($params['relname2']) ? $params['relname2'] : '';
        $model->relname3 = isset($params['relname3']) ? $params['relname3'] : '';
        $model->age = isset($params['age']) ? $params['age'] : 0;
        $model->song = isset($params['song']) ? $params['song'] : '';
        $model->yuanchuang = isset($params['yuanchuang']) ? $params['yuanchuang'] : 0;
        $model->video = isset($params['video']) ? $params['video'] : '';
        $model->email = isset($params['email']) ? $params['email'] : '';
        $model->mobile = isset($params['mobile']) ? $params['mobile'] : '';


        $model->source = isset($params['source']) ? $params['source'] : '';
        if ($params['source'] == '0') {

            $model->source = isset($params['source_other']) ? $params['source_other'] : '';
        }

        $rt = $model->save();

        if ($rt) {

            if (!isset($params['isMobile'])) {
//                return redirect('/action/success');
                return view('web.success');
            } else {
//                return redirect('/mobile/action/success');
                return view('h5.success');
            }

        }
    }


    public function getSuccess()
    {
        return view('web.success');
    }

}
